﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.UI.Dropdown;

public class SliderSettingView : MonoBehaviour
{
    public Dropdown maxNumber;
    public Dropdown minNumber;
    public Dropdown returnNumber;
    public Toggle isReturn;
    public Button back;

    int maxNumberData = 3;
    int minNumberData = -3;
    int returnNumberData = 0; 

    // Start is called before the first frame update
    void Start()
    {
        //初始化赋值
        maxNumberData = GameData.maxNumber;
        minNumberData = GameData.minNumber;
        returnNumberData = GameData.returnNumber;

        //初始化控件
        //归位开关
        isReturn.isOn = GameData.autoReturn;
        //最大数字列表
        maxNumber.options.Clear();
        for (int i = minNumberData + 1; i <= 3; i++)
        {
            OptionData option = new OptionData(i.ToString());
            maxNumber.options.Add(option);
            if (i == maxNumberData)
            {
                maxNumber.value = -1;
                maxNumber.value = i - (minNumberData + 1);
            }
        }

        //最小数字列表
        minNumber.options.Clear();
        for (int i = -3; i < maxNumberData; i++)
        {
            OptionData option = new OptionData(i.ToString());
            minNumber.options.Add(option);
            if (i == minNumberData)
            {
                minNumber.value = -1;
                minNumber.value = i + 3;
            }
        }
        //归位数字列表
        returnNumber.options.Clear();
        for (int i = minNumberData; i <= maxNumberData; i++)
        {
            OptionData option = new OptionData(i.ToString());
            returnNumber.options.Add(option);
            if (i == returnNumberData)
            {
                returnNumber.value = -1;
                returnNumber.value = i + 3;
            }
        }

        //绑定事件
        isReturn.onValueChanged.AddListener((value) => {
            GameData.autoReturn = value;
        });
        back.onClick.AddListener(() => {
            GameData.maxNumber = maxNumberData;
            GameData.minNumber = minNumberData;
            GameData.returnNumber = returnNumberData;
            this.gameObject.SetActive(false);
        });
        maxNumber.onValueChanged.AddListener((value)=> {
            maxNumberData = int.Parse(maxNumber.captionText.text);
            //动态调整范围
            minNumber.options.Clear();
            for (int i = -3; i < maxNumberData; i++)
            {
                OptionData option = new OptionData(i.ToString());
                minNumber.options.Add(option);
                if (i == minNumberData)
                {
                    minNumber.value = i + 3;
                }
            }

            returnNumber.options.Clear();
            for (int i = minNumberData; i <= maxNumberData; i++)
            {
                OptionData option = new OptionData(i.ToString());
                returnNumber.options.Add(option);
            }
            returnNumber.value = returnNumber.options.Count / 2;//居中
        });
        minNumber.onValueChanged.AddListener((value)=> {
            minNumberData = int.Parse(minNumber.captionText.text);
            //动态调整范围
            maxNumber.options.Clear();
            for (int i = minNumberData + 1; i <= 3; i++)
            {
                OptionData option = new OptionData(i.ToString());
                maxNumber.options.Add(option);
                if (i == maxNumberData)
                {
                    maxNumber.value = i - (minNumberData + 1);
                }
            }

            returnNumber.options.Clear();
            for (int i = minNumberData; i <= maxNumberData; i++)
            {
                OptionData option = new OptionData(i.ToString());
                returnNumber.options.Add(option);
            }
            returnNumber.value = returnNumber.options.Count / 2;//居中
        });
        returnNumber.onValueChanged.AddListener((value)=> {
            returnNumberData = int.Parse(returnNumber.captionText.text);
        });
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
